﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EBEInventory_DAL;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
    InventoryEntities ctx = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        
         ctx = InitConnection.Initialize();



         GridInventory.DataSource = InvOps.GetInventories(ctx);

        if (ViewState["BindingMethod"] != null)
            GridSubLoc.DataSource = ViewState["BindingMethod"];

        else
            GridSubLoc.DataSource = SubInvOps.GetSubInventories(ctx);

        Page.DataBind();

    }
    
    protected void GridInventory_SelectedIndexChanged(object sender, EventArgs e)
    {
        Inventory inv = new Inventory();
        inv.ID = Convert.ToInt32(GridInventory.SelectedRow.Cells[1].Text);
        inv.Name = GridInventory.SelectedRow.Cells[2].Text;
        inv.Address = GridInventory.SelectedRow.Cells[3].Text;
        inv.Phone = GridInventory.SelectedRow.Cells[4].Text;
        inv.Mobile = GridInventory.SelectedRow.Cells[5].Text;
        inv.Notes = GridInventory.SelectedRow.Cells[6].Text;

        Session["SelectedInventory"] = inv;

        GridSubLoc.DataSource = ViewState["BindingMethod"] = SubInvOps.GetSubInvByInvID(inv.ID, ctx);
        
       GridSubLoc.DataBind();
    }

    protected void btn_SearchSubLoc_Click(object sender, EventArgs e)
    {
       // string a = txt_SubLocId.Text;
        string b = txt_SubLocName.Text;
        if ( !string.IsNullOrEmpty(b))
        {
            GridSubLoc.DataSource = ViewState["BindingMethod"] = SubInvOps.GetSubInventoryByID(b, ctx);

            GridSubLoc.DataBind();
        }
    }
    
    protected void GridInventory_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {

        GridInventory.PageIndex = e.NewPageIndex;
        GridInventory.DataBind();
    }
    
    protected void GridSubLoc_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridSubLoc.PageIndex = e.NewPageIndex;
        GridSubLoc.DataBind();
    }
}